
package coffee;

import gui.model.DrinkModel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 * @author bwolff
 */
public class User {
    
    private int id;
    private String name;
    private int weight;
    private String image;
    private String room;
    private DrinkModel.DrinkRepresantation favouriteTea;
    private DrinkModel.DrinkRepresantation favouriteCoffee;
    // String[] in Form timestamp, drinkcode, price, caffeine
    private ArrayList<Statistics> statistic = new ArrayList<>();
    
    public User(int id){        
        this.loadUser(id);
    }
    
    public User(String name, int weight, String room){        
        this.id = (int) (System.currentTimeMillis() / 1000L);
        this.name = name;
        this.weight = weight;
        this.room = room;
        //this.image = "../userData/1.png";
        this.image = "";
        this.saveUser();
    }
    
    public static List<User> getUsers() {
        List<User> output = new ArrayList<>();
        File userDir = new File("userData");
        userDir.mkdirs();
        for(File f : userDir.listFiles()) {
            if(f.getName().endsWith(".ser") && f.isFile()) {
                String id = f.getName().substring(0, f.getName().indexOf(".ser"));
                output.add(new User(Integer.parseInt(id)));
            }
        }
        
        return output;
    }
    
    public boolean saveUser(){
         try {
             
            // create a new file with an ObjectOutputStream
            FileOutputStream out = new FileOutputStream("userData/"+String.valueOf(this.id)+".ser");
            ObjectOutputStream oout = new ObjectOutputStream(out);

            // write something in the file
            oout.writeObject(this.id);
            oout.writeObject(this.name);
            oout.writeObject(this.weight);
            oout.writeObject(this.image);  
            oout.writeObject(this.room);
            oout.writeObject(this.favouriteCoffee);
            oout.writeObject(this.favouriteTea);
            oout.writeObject(this.statistic);
            // close the stream
            oout.close();
            return true;
      } catch (Exception ex) {
         ex.printStackTrace();
         return false;
      }
    }

    public boolean loadUser(int id){
         try {
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream("userData/"+String.valueOf(id)+".ser"));
            
            this.id = (int) ois.readObject();
            this.name = (String) ois.readObject();
            this.weight = (int) ois.readObject();
            this.image = (String) ois.readObject();
            this.room = (String) ois.readObject();
            this.favouriteCoffee = (DrinkModel.DrinkRepresantation) ois.readObject();
            this.favouriteTea = (DrinkModel.DrinkRepresantation) ois.readObject();
            this.statistic = (ArrayList<Statistics>) ois.readObject();
            return true;
         } catch (Exception ex) {
            ex.printStackTrace();
            return false;
      }
    }
    
    public void setName(String name){
        this.name = name;
    }
    
    public void setWeight(int weight){
        this.weight = weight;
    }
    
    public void setRoom(String room){
        this.room = room;
    }
    
    public void setStatistic(ArrayList<Statistics> statistic){
        this.statistic = statistic;
    }
    
    public void setFavouriteCoffee(DrinkModel.DrinkRepresantation drink) {
        favouriteCoffee = drink;
    }
    
    public void setFavouriteTea(DrinkModel.DrinkRepresantation drink) {
        favouriteTea = drink;
    }
    
    public int getId(){
        return this.id;
    }
    
    public String getName(){
        return this.name;
    }
    
    public int getWeight(){
        return this.weight;
    }
    
    public String getRoom(){
        return this.room;
    }
    
    public DrinkModel.DrinkRepresantation getFavouriteCoffee() {
        return favouriteCoffee;
    }
    
    public DrinkModel.DrinkRepresantation getFavouriteTea() {
        return favouriteTea;
    }
    
    public ArrayList<Statistics> getStatistic(){
        return this.statistic;
    }
    
    public int getCoffeinSum24(){
        int sum = 0;
        Timestamp tStamp = new Timestamp(System.currentTimeMillis());
        long ts = tStamp.getTime();
        long ts2 = ts - 60*60*24*1000;        
        for(Statistics val : statistic){
            long tmpTs = val.getTimestamp();
            if(tmpTs > ts2)
                sum += val.getCaffein();
        }
        return sum;
    }
    
    public int getCoffeinSum(long ts){
        int sum = 0;    
        for(Statistics val : statistic){
            long tmpTs = val.getTimestamp();
            if(tmpTs > ts)
                sum += val.getCaffein();
        }
        return sum;
    }
    
    public ArrayList<String[]> getDrinkList(long ts){
        ArrayList<String[]> drinkList = new ArrayList<>();        
        for(Statistics val : statistic){
            long tmpTs = val.getTimestamp();
            //System.out.print("tmpTs "+tmpTs+"ts "+ts);
            if(tmpTs > ts){
                String[] str = new String[2];
                str[0] = String.valueOf(val.getPrice());
                str[1] = String.valueOf(val.getCaffein());
                drinkList.add(str);
            }            
        }
        return drinkList;
    }
    
   
    // for Debug
    public void print(){
        System.out.println(String.valueOf("Id: "+id));
        System.out.println(String.valueOf("Name: "+name));
        System.out.println(String.valueOf("Weight: "+weight));
        System.out.println(String.valueOf("Image: "+image));
        System.out.println(String.valueOf("Room: "+room));
        System.out.println(String.valueOf("favCoffee: "+favouriteCoffee));
        System.out.println(String.valueOf("favTea: "+favouriteTea));        
        for (Statistics str : statistic) {
            System.out.println("lala");
            System.out.println(str.getTimestamp()+" "+str.getDrink()+" "+str.getPriceAsString()+" "+str.getCaffein());
        }
    }
}
